const KoaRouter = require('koa-router')
const HttpClient = require('../util/HttpClient')
const Template = require('../util/Template')

const router = new KoaRouter();
const modules = require('../../modules');

router.get('/setting/customer/fields', async ctx => {
  let script = ['/setting.customer.fields.js'];
  let modConfig = modules['setting.customer.fields'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/setting/customer/fields', 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('客户设置', body, script, modConfig.template, ctx)
});

router.get('/setting/product/fields', async ctx => {
  let script = ['/setting.product.fields.js'];
  let modConfig = modules['setting.product.fields'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/setting/product/fields', 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('产品设置', body, script, modConfig.template, ctx)
});

router.get('/setting/performance/v2/rule', async ctx => {
  let script = ['/setting.performance.rule.js'];
  let modConfig = modules['setting.performance.rule'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/setting/performance/v2/rule', 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('绩效规则', body, script, modConfig.template, ctx)
});

router.get('/setting/performance/v2/report', async ctx => {
  let script = ['/setting.performance.report.js'];
  let modConfig = modules['setting.performance.report'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/setting/performance/v2/report', 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('报表设置', body, script, modConfig.template, ctx)
});

router.get('/setting/task/taskSet', async ctx => {
  let modConfig = modules['setting.task.manage'];
  let script = ['/setting.task.manage.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('工单设置', body, script, modConfig.template, ctx)
});
router.get('/setting/taskType/manage', async ctx => {
  let modConfig = modules['setting.taskType.manage'];
  let script = ['/setting.taskType.manage.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;

  ctx.body = await Template.renderWithHtml('工单类型设置', body, script, modConfig.template, ctx)
});

router.get('/setting/task/taskFormSet', async ctx => {
  let modConfig = modules['setting.task.flow'];
  let script = ['/setting.task.flow.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('工单流程设置', body, script, modConfig.template, ctx)
});

router.get('/setting/task/cardManage', async ctx => {
  let modConfig = modules['setting.task.additional'];
  let script = ['/setting.task.additional.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('附加组件设置', body, script, modConfig.template, ctx)
});

router.get('/setting/task/cardFormfields', async ctx => {
  let modConfig = modules['setting.task.addcard_fields'];
  let script = ['/setting.task.addcard_fields.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('附加组件表单设置', body, script, modConfig.template, ctx)
});

router.get('/setting/task/cardHoursRecord', async ctx => {
  let modConfig = modules['setting.task.hoursrecord'];
  let script = ['/setting.task.hoursrecord.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('工单设置', body, script, modConfig.template, ctx)
});

router.get('/setting/task/cardSpareService', async ctx => {
  let modConfig = modules['setting.task.spareservice'];
  let script = ['/setting.task.spareservice.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('备件服务清单', body, script, modConfig.template, ctx)
});

// 物料申领附加组件
router.get('/setting/task/cardMaterialApplyService', async ctx => {
  let modConfig = modules['setting.task.materialapply'];
  let script = ['/setting.task.materialapply.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('物料申领设置', body, script, modConfig.template, ctx)
});

// #region 服务事件类型
//  服务事件类型列表
router.get('/setting/serviceStation/eventType', async ctx => {
  let modConfig = modules['setting.serviceEventType.manage'];
  let script = ['/setting.serviceEventType.manage.js'];
  let {
    url,
    headers
  } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, {
    headers
  });
  let body = result.body;

  ctx.body = await Template.renderWithHtml('事件类型设置', body, script, modConfig.template, ctx)
});

//  服务事件类型流程设置
router.get('/setting/serviceStation/eventForm', async ctx => {
  let modConfig = modules['setting.serviceEventType.flow'];
  let script = ['/setting.serviceEventType.flow.js'];
  let {
    url,
    headers
  } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, {
    headers
  });
  let body = result.body;

  ctx.body = await Template.renderWithHtml('事件类型设置', body, script, modConfig.template, ctx)
});

//  服务事件附加组件
router.get('/setting/serviceStation/cardManage', async ctx => {
  let modConfig = modules['setting.serviceEventType.additional'];
  let script = ['/setting.serviceEventType.additional.js'];
  let {
    url,
    headers
  } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, {
    headers
  });
  let body = result.body;

  ctx.body = await Template.renderWithHtml('事件附加组件设置', body, script, modConfig.template, ctx)
});

//  服务事件附加组件表单设置
router.get('/setting/serviceStation/card/view', async ctx => {
  let modConfig = modules['setting.serviceEventType.additional.additionalFormView'];
  let script = ['/setting.serviceEventType.additional.additionalFormView.js'];
  let {
    url,
    headers
  } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, {
    headers
  });
  let body = result.body;

  ctx.body = await Template.renderWithHtml('事件附加组件表单设置', body, script, modConfig.template, ctx)
});

// #endregion 服务事件类型

// 客户附加组件
router.get('/setting/customer/additional', async ctx=>{
  let modConfig = modules['setting.customer.additional'];
  let script = ['/setting.customer.additional.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('附加组件设置', body, script, modConfig.template, ctx)
});

// 客户附加组件表单设置
router.get('/setting/customer/cardFormfields', async ctx => {
  let modConfig = modules['setting.customer.addcard_fields'];
  let script = ['/setting.customer.addcard_fields.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('附加组件表单设置', body, script, modConfig.template, ctx)
});

router.get('/setting/product/productType', async ctx => {
  let modConfig = modules['setting.product.productType'];
  let script = ['/setting.product.productType.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('产品管理设置', body, script, modConfig.template, ctx)
});

// 产品附加组件
router.get('/setting/product/additional', async ctx => {
  let modConfig = modules['setting.product.additional'];
  let script = ['/setting.product.additional.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('附加组件设置', body, script, modConfig.template, ctx)
});

router.get('/setting/serviceStation/partShop', async ctx => {
  let script = ['/system.mall.index.js'];
  let modConfig = modules['system.mall.index'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/setting/serviceStation/partShop', 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('商品列表', body, script, modConfig.template, ctx)
});

// TODO 服务备注 等表单设计器迁移后 需要在service-desk项目实现
router.get('/setting/serviceRemark', async ctx => {
  let modConfig = modules['serviceRemark.edit'];
  let script = ['/serviceRemark.edit.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;

  ctx.body = await Template.renderWithHtml('服务备注表单设置', body, script, modConfig.template, ctx)
});

// router.get('/setting/serviceStation/deskSetting', async ctx => {
//   let modConfig = modules['setting.service_desk.desk_setting'];
//   let script = ['/setting.service_desk.desk_setting.js'];
//   let { url, headers } = ctx.request;
//   let result = await HttpClient.request(url, 'get', null, { headers });
//   let body = result.body;

//   ctx.body = await Template.renderWithHtml('服务台设置', body, script, modConfig.template, ctx)
// });

// router.get('/setting/serviceStation/im', async ctx => {
//   let modConfig = modules['setting.service_desk.im'];
//   let script = ['/setting.service_desk.im.js'];
//   let { url, headers } = ctx.request;
//   let result = await HttpClient.request(url, 'get', null, { headers });
//   let body = result.body;

//   ctx.body = await Template.renderWithHtml('在线客服设置', body, script, modConfig.template, ctx)
// });

// 服务台设置相关入口
// router.get('/setting/serviceStation/main/', async ctx => {
//   ctx.response.redirect('http://localhost:3000/setting/serviceStation/main/');
// let modConfig = modules['setting.service_desk'];
// let script = ['/setting.service_desk.js'];
// let { url, headers } = ctx.request;
// let result = await HttpClient.request(url, 'get', null, { headers });
// let body = result.body;

// ctx.body = await Template.renderWithHtml('服务台设置', body, script, modConfig.template, ctx)
// });

router.get('/setting/cost/serviceItem', async ctx => {
  let modConfig = modules['setting.service.item'];
  let script = ['/setting.service.item.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('服务项目设置', body, script, modConfig.template, ctx)
});

// 备件设置
router.get('/setting/sparepart2/repertory', async ctx => {
  let modConfig = modules['setting.sparepart.repertory'];
  let script = ['/setting.sparepart.repertory.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('备件库管理', body, script, modConfig.template, ctx)
});
router.get('/setting/sparepart/instock', async ctx => {
  let modConfig = modules['setting.sparepart.instock'];
  let script = ['/setting.sparepart.instock.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('备件选项', body, script, modConfig.template, ctx)
});

router.get('/setting/customer/tag', async ctx => {
  let modConfig = modules['setting.customer.tag'];
  let script = ['/setting.customer.tag.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('客户标签设置', body, script, modConfig.template, ctx)
});

router.get('/setting/info/wiki', async ctx => {
  let modConfig = modules['setting.info.wiki'];
  let script = ['/setting.info.wiki.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('知识库设置', body, script, modConfig.template, ctx)
});

router.get('/setting/product/register/fields', async ctx => {
  let script = ['/setting.product.register.fields.js'];
  let modConfig = modules['setting.product.register.fields'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/setting/product/register/fields', 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('产品注册设置', body, script, modConfig.template, ctx)
});

router.get('/setting/contract/flow', async ctx => {
  let modConfig = modules['setting.contract.flow'];
  let script = ['/setting.contract.flow.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;

  ctx.body = await Template.renderWithHtml('合同管理设置', body, script, modConfig.template, ctx)
})

router.get('/setting/contract/manage', async ctx => {
  let modConfig = modules['setting.contract.manage'];
  let script = ['/setting.contract.manage.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;

  ctx.body = await Template.renderWithHtml('合同管理设置', body, script, modConfig.template, ctx)
});

router.get('/setting/common/usercard', async ctx => {
  let modConfig = modules['setting.common.user_card'];
  let script = ['/setting.common.user_card.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;

  ctx.body = await Template.renderWithHtml('人员卡片配置', body, script, modConfig.template, ctx)
});

// TODO 服务商管理 等表单设计器迁移后 需要在resourse-manage项目实现
router.get('/setting/service/provider/fields', async ctx => {
  let modConfig = modules['setting.service.provider.fields'];
  let script = ['/setting.service.provider.fields.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  const type = ctx.request.query.type;
  ctx.body = await Template.renderWithHtml(type === 'provider' ? '服务商表单设置' : '工程师表单设置', body, script, modConfig.template, ctx)
});

// 门户商城设置
router.get('/setting/linkcMall/manage', async ctx => {
  let modConfig = modules['setting.linkcMall.manage'];
  let script = ['/setting.linkcMall.manage.js'];
  let { url, headers } = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('商城设置', body, script, modConfig.template, ctx)
});

// 消息渠道设置
router.get('/setting/message/channel', async ctx => {
  let modConfig = modules['setting.message.channel'];
  let script = ['/setting.message.channel.js'];
  ctx.body = await Template.renderWithHtml('消息渠道设置', {}, script, modConfig.template, ctx)
});

module.exports = router;
